//
//  PainterIndexGameMatrix.m
//  p4
//
//  Created by CaoJun on 15/11/9.
//  Copyright © 2015年 com.baidao. All rights reserved.
//

#import "PainterIndexGameMatrix.h"
#import "GameMatrix.h"

using namespace std;
using namespace ytx;

@implementation PainterIndexGameMatrix

- (void)__attribute__((annotate("oclint:suppress[long method]"))) __attribute__((annotate("oclint:suppress[high ncss method]")))drawRect:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition
{
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();
    Quotation * data = indexPosition->getData();
    
    vector<std::vector<double> > & indexData = data->indexData;
    
    if (indexData.size() == 0 || indexData[0].size() == 0) {
        return;
    }
    
    vector<std::vector<double> > & lineData = data->lineData;
    
    std::vector<double> & types = indexData[0];
    std::vector<double> & bound1 = lineData[0];
    std::vector<double> & bound2 = lineData[1];
    
    string backgroundLineColor = "", backgroundColor = "", kColor = "", triangleColor = "", boundColor = "";
    float zoomX, tempX = rect.getBeginX(), halfX = rect.getBeginX(), beginY = rect.getBeginY(), height = rect.getRectHeight(),
    triangleVertexX = 0.0, triangleVertexY = 0.0, triangleWaistX1 = 0.0, triangleWaistY1 = 0.0, triangleWaistX2 = 0.0, triangleWaistY2 = 0.0;
    
    int startIndex = indexPosition->getStartDataIndex(), endIndex = indexPosition->getEndDataIndex();
    
    zoomX = rect.getZoomX();
    
    halfX += zoomX * 0.5;
    
    GameMatrixType::GameMatrixType type, pretype = (GameMatrixType::GameMatrixType)types[startIndex];
    
    size_t index;
    
    KLineItem item;
    
    float HighY, LowY, BeginY, EndY, UpY, DownY;
    float bound1Y, bound2Y;
    float lineWidth = style.quoteLineWidth, lineWidthHalf = lineWidth * 0.5f;
    
    NSMutableArray * bound1Points = [NSMutableArray array];
    NSMutableArray * bound2Points = [NSMutableArray array];
    
    [ctx setLineWidth: lineWidth];
    for (std::vector<double>::iterator it = types.begin() + startIndex; it != types.begin() + endIndex; ++it) {
        
        type = (GameMatrixType::GameMatrixType)((int)*it);
        index = it-types.begin();
        
        item = data->kLineData[index];
        
        BeginY = rect.getPxY(item.open);
        EndY = rect.getPxY(item.close);
        HighY = rect.getPxY(item.high);
        LowY = rect.getPxY(item.low);
        
        if (type == GameMatrixType::Long || type == GameMatrixType::Short) {
            if (type == GameMatrixType::Long) {
                backgroundColor = "#37fa8886";
                kColor = "#254b7c57";
                [ctx fillRectWithX:tempX withY:beginY withWidth:zoomX withHeight:height withFillColor: [NSString stringWithUTF8String:backgroundColor.c_str()] ];
            }
            else if (type == GameMatrixType::Short){
                backgroundColor = "#254b7c57";
                kColor = "#254b7c57";
                [ctx fillRectWithX:tempX withY:beginY withWidth:zoomX withHeight:height withFillColor: [NSString stringWithUTF8String:backgroundColor.c_str()] ];
            }
            //画bound
            bound1Y = rect.getPxY(bound1[index]);
            bound2Y = rect.getPxY(bound2[index]);
            
            if (type == pretype){
                [bound1Points addObject: [NSValue valueWithCGPoint:CGPointMake(halfX , bound1Y)]];
                [bound2Points insertObject:[NSValue valueWithCGPoint:CGPointMake(halfX , bound2Y)] atIndex:0];
            }
            
            if (type != pretype) {
                if (type == GameMatrixType::Long) {
                    backgroundLineColor = style.GameMatrixTypeBackgroundLineLong;
                }
                else if (type == GameMatrixType::Short) {
                    backgroundLineColor = style.GameMatrixTypeBackgroundLineShort;
                }
                [ctx setStrokeStyle: [NSString stringWithUTF8String:backgroundLineColor.c_str()] ];
                [ctx drawLineWithX1:tempX  withY1:0 withX2:tempX withY2:height];
            }
            
            

        }
        
        //到最后条也要画
        if ((type != pretype && pretype != GameMatrixType::Flat ) || (it == types.begin() + endIndex - 1) ) {
            //画bound
            bound1Y = rect.getPxY(bound1[index]);
            bound2Y = rect.getPxY(bound2[index]);

            [bound1Points addObjectsFromArray:bound2Points];
            
            if (pretype == GameMatrixType::Short) {
                boundColor = "#ffaad9be";
            }
            else if (pretype == GameMatrixType::Long) {
                boundColor = "#c5fa9794";
            }
            
            if (pretype != GameMatrixType::Flat) {
                [ctx fillPath:bound1Points withFillColor:[NSString stringWithUTF8String:boundColor.c_str()]];
            }
            
            [bound1Points removeAllObjects];
            [bound2Points removeAllObjects];
            
            [bound1Points addObject: [NSValue valueWithCGPoint:CGPointMake(halfX , bound1Y)]];
            [bound2Points insertObject:[NSValue valueWithCGPoint:CGPointMake(halfX , bound2Y)] atIndex:0];
        }
        
        pretype = type;
        
        tempX += zoomX;
        halfX += zoomX;
    }
    
    //最后画K线
    tempX = rect.getBeginX();
    halfX = rect.getBeginX() + zoomX * 0.5;
    pretype = (GameMatrixType::GameMatrixType)types[startIndex];
    for (std::vector<double>::iterator it = types.begin() + startIndex; it != types.begin() + endIndex; ++it) {
        
        type = (GameMatrixType::GameMatrixType)((int)*it);
        index = it-types.begin();
        
        item = data->kLineData[index];
        
        BeginY = rect.getPxY(item.open);
        EndY = rect.getPxY(item.close);
        HighY = rect.getPxY(item.high);
        LowY = rect.getPxY(item.low);
        
        if (type == GameMatrixType::Long) {
            kColor = "#ff404a";
        }
        else if (type == GameMatrixType::Short) {
            kColor = "#11c553";
        }
        else if (type == GameMatrixType::Flat) {
            if(item.open > item.close)
            {
                kColor = style.defaultGreen;
            }
            else if(item.open < item.close){
                kColor = style.defaultRed;
            }
            else {
                kColor = style.defaultWhite;
            }
        }
   
        //画K线
        [ctx setStrokeStyle: [NSString stringWithUTF8String:kColor.c_str()] ];
        if(item.open > item.close)
        {
            UpY = BeginY;
            DownY = EndY;
            [ctx fillRectWithX1:tempX + lineWidthHalf withY1:EndY withX2:tempX + zoomX - lineWidthHalf withY2:BeginY withFillColor:[NSString stringWithUTF8String:kColor.c_str()]];
        }
        else{//涨的画空心K线
            UpY = EndY;
            DownY = BeginY;
            [ctx drawLineWithX1:tempX + lineWidth withY1:BeginY withX2:tempX + lineWidth withY2:EndY];
            [ctx drawLineWithX1:tempX + lineWidth withY1:EndY withX2:tempX + zoomX - lineWidth withY2:EndY];
            [ctx drawLineWithX1:tempX + zoomX - lineWidth withY1:EndY withX2:tempX + zoomX - lineWidth withY2:BeginY];
            [ctx drawLineWithX1:tempX + zoomX - lineWidth withY1:BeginY withX2:tempX + lineWidth withY2:BeginY];
        }
        
        //画三角
        if (type != pretype && type != GameMatrixType::Flat)
        {
            switch (type) {
                case GameMatrixType::Long:
                    triangleColor = style.GameMatrixTriangleVertexTypeLong;
                    triangleVertexY = LowY + zoomX;
                    triangleWaistY1 = triangleVertexY + zoomX;
                    triangleWaistY2 = triangleVertexY + zoomX;
                    break;
                case GameMatrixType::Short:
                    triangleColor = style.GameMatrixTriangleVertexTypeShort;
                    triangleVertexY = HighY - zoomX;
                    triangleWaistY1 = triangleVertexY - zoomX;
                    triangleWaistY2 = triangleVertexY - zoomX;
                    break;
                default:
                    break;
            }
            
            triangleWaistX1 = tempX;
            triangleWaistX2 = tempX+zoomX;
            triangleVertexX = halfX;
            NSMutableArray * points = [NSMutableArray array];
            
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(triangleVertexX, triangleVertexY)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(triangleWaistX1, triangleWaistY1)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(triangleWaistX2, triangleWaistY2)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(triangleVertexX, triangleVertexY)]];
            
            [ctx fillPath:points withFillColor:[NSString stringWithUTF8String:triangleColor.c_str()]];
            
        }
        
        pretype = type;
        
        [ctx drawLineWithX1:halfX withY1:HighY withX2:halfX withY2:UpY];
        [ctx drawLineWithX1:halfX withY1:LowY withX2:halfX withY2:DownY];
        
        tempX += zoomX;
        halfX += zoomX;
    }
}


@end
